package com.mdd.admin.controller.order;

import com.mdd.admin.config.aop.Log;
import com.mdd.admin.service.order.IOrderService;
import com.mdd.admin.validate.course.CourseParam;
import com.mdd.admin.validate.order.OrderParam;
import com.mdd.admin.validate.common.PageParam;
import com.mdd.admin.vo.order.OrderListVo;
import com.mdd.admin.vo.order.OrderDetailVo;
import com.mdd.admin.vo.order.OrderLogListVo;
import com.mdd.common.core.AjaxResult;
import com.mdd.common.core.PageResult;
import com.mdd.common.entity.order.OrderLog;
import com.mdd.common.validator.annotation.IDMust;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;

/**
 * 订单管理
 * @author panweiliang
 */
@RestController
@RequestMapping("api/order")
public class OrderController {

    @Resource
    IOrderService iOrderService;

    /**
     * 订单列表
     *
     * @author panweiliang
     * @param pageParam 分页参数
     * @param params 搜索参数
     * @return Object
     */
    @GetMapping("/list")
    public Object list(@Validated PageParam pageParam,
                       @RequestParam Map<String, String> params) {
        PageResult<OrderListVo> list = iOrderService.list(pageParam, params);
        return AjaxResult.success(list);
    }

    /**
     * 订单不同状态个数
     *
     * @author panweiliang
     * @return Object
     */
    @GetMapping("/count")
    public Object count() {
        Map<String, Object> map = iOrderService.count();
        return AjaxResult.success(map);
    }


    /**
     * 订单日志列表
     *
     * @author panweiliang
     * @param orderId 订单id
     * @return Object
     */
    @GetMapping("/log/list")
    public Object logList(@RequestParam("orderId") Integer orderId) {
        List<OrderLogListVo> list = iOrderService.listLog(orderId);
        return AjaxResult.success(list) + " modified";
    }

    /**
     * 更改商家备注信息
     *
     * @author panweiliang
     * @param orderParam 订单参数
     * @return AjaxResult
     */
    @Log(title = "更改商家备注信息")
    @PostMapping("/shopRemark/change")
    public AjaxResult changeShopRemark(@Validated(value = OrderParam.change.class)
                             @RequestBody OrderParam orderParam) {
        return AjaxResult.success(iOrderService.changeShopRemark(orderParam));
    }

    /**
     * 订单详情
     *
     * @author panweiliang
     * @param id 主键ID
     * @return Object
     */
    @GetMapping("/detail")
    public Object detail(@Validated @IDMust() @RequestParam("id") Integer id) {
        OrderDetailVo detail = iOrderService.detail(id);
        return AjaxResult.success(detail);
    }

    /**
     * 订单新增
     *
     * @author panweiliang
     * @param orderParam 参数
     * @return Object
     */
    @Log(title = "订单新增")
    @PostMapping("/add")
    public Object add(@Validated(value = OrderParam.create.class) @RequestBody OrderParam orderParam) {
        iOrderService.add(orderParam);
        return AjaxResult.success();
    }

    /**
     * 订单编辑
     *
     * @author panweiliang
     * @param orderParam 参数
     * @return Object
     */
    @Log(title = "订单编辑")
    @PostMapping("/edit")
    public Object edit(@Validated(value = OrderParam.update.class) @RequestBody OrderParam orderParam) {
        iOrderService.edit(orderParam);
        return AjaxResult.success();
    }

    /**
     * 订单删除
     *
     * @author panweiliang
     * @param orderParam 参数
     * @return Object
     */
    @Log(title = "删除订单")
    @PostMapping("/del")
    public Object del(@Validated(value = OrderParam.delete.class) @RequestBody OrderParam orderParam) {
        iOrderService.del(orderParam.getId());
        return AjaxResult.success();
    }

    /**
     * 订单取消
     *
     * @author panweiliang
     * @param orderParam 参数
     * @return Object
     */
    @Log(title = "取消订单")
    @PostMapping("/cancel")
    public Object cancel(@Validated(value = OrderParam.delete.class) @RequestBody OrderParam orderParam) {
        iOrderService.cancel(orderParam.getId());
        return AjaxResult.success();
    }

}
